package com.sc.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.sc.pojo.Field;
import com.sc.pojo.User;
import com.sc.services.FieldServices;
import com.sc.services.UserServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @description:
 * @author: sc
 * @create: 2023-01-04 16:46
 **/
@WebServlet("/userLogin")
public class userLoginServlet extends HttpServlet {

    private UserServices userServices = new UserServices();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();




        User user = userServices.getUser(userid);
        if(user!=null){
            if(userServices.isRightPassword(password,user.getPassword())){
                session.setAttribute("user",user);
                response.sendRedirect("/FieldBookSystem/User_Wel.html");
            }
            else{
                request.setAttribute("passwordwro_mes","密码错误");
                request.getRequestDispatcher("login.jsp").forward(request,response);
            }
        }
        else {
            request.setAttribute("usernamewro_mes","账号错误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}